package com.dai.zhenfujin.visitingcard.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.adapter.VisitingTypeAdpter;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.bean.TypeListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.view.SodukuGridView;
import com.dai.zhenfujin.visitingcard.SearchActiviy;
import com.dai.zhenfujin.visitingcard.adapter.VisitingCardAdapter2;
import com.dai.zhenfujin.visitingcard.app.ParticularsPageActivity;
import com.dai.zhenfujin.visitingcard.bean.VisitingCardBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 婚的页面
 */
public class MarryActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvTitleName;
    private ImageView imageView,iv_no_msg,title_search;
    private ListView listView;
    private FrameLayout fl_img;
    private View view;

    private List<VisitingCardBean> beanList;

    private VisitingCardAdapter2 adapter;
    private int page = 0;
    private int number = 10;
    private VisitingTypeAdpter typeListAdapter;
    private List<TypeListBean.TypelistBean> typeList = new ArrayList<>();


    private TextView tvBlindDate, tvWedding, tvArticles, tvhoneymoon;
    private List<ProListBean.ProListBean2> proListBBean = new ArrayList<>();
    private SmartRefreshLayout refreshLayout;
    private SodukuGridView rv_type;
    private String t_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marry);
        // view  = View.inflate(this,R.layout.marry_listview_head,null);

        initView();
        initListener();
        onRefresh();
        initRefresh();

        adapter = new VisitingCardAdapter2(this, proListBBean);
        listView.setAdapter(adapter);
        listView.setVerticalScrollBarEnabled(false);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            if (null != proListBBean && position > 0) {
                Intent intent = new Intent(this, ParticularsPageActivity.class);
                intent.putExtra("pro_id", proListBBean.get(position - 1).getPro_id());
                intent.putExtra("type_id", proListBBean.get(position - 1).type_id);
                startActivity(intent);
            }
        });
    }



    private void onRefresh() {
        page = 1;
        getProductList(true);
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("type_id", getIntent().getStringExtra("type_id"));
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.TYPELIST, params, new CallBack<TypeListBean>() {
            @Override
            public void onSuccess(TypeListBean resultBean) {
                if (resultBean.getTypeList().size() > 0) {
                    typeList.clear();
                    typeList.addAll(resultBean.getTypeList());
                    typeListAdapter.notifyDataSetChanged();
                }else {
                    rv_type.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initRefresh() {
        refreshLayout.setOnRefreshListener(refreshLayout -> refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                page = 1;
                getProductList(true);
                refreshLayout.finishRefresh();
                refreshLayout.resetNoMoreData();
            }
        }, 1500));

        refreshLayout.setOnLoadmoreListener(refreshlayout -> getProductList(false));
    }

    private void getProductList(boolean isRefresh) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        if (TextUtils.isEmpty(t_id)){
            params2.put("type_id", getIntent().getStringExtra("type_id"));
        }else {
            params2.put("type_id", t_id);
        }

        params2.put("words", "");
        params2.put("page", page + "");
        params2.put("number", number + "");
        params2.put("loc_x", SPUtils.getString(this, "longitude"));
        params2.put("loc_y", SPUtils.getString(this, "latitude"));
        params.put("req_bd", params2);
        HttpClient.post(this, Constants.PRODUCTLIST, params, new CallBack<ProListBean>() {
            @Override
            public void onSuccess(ProListBean proListBean) {
                if (isRefresh) {
                    proListBBean.clear();
                    if (proListBean.getProList().size() == 0){
                        proListBBean.addAll(proListBean.getProList());
                        adapter.notifyDataSetChanged();
                        iv_no_msg.setVisibility(View.VISIBLE);
                        return;
                    }
                    page = 2;
                } else {
                    page++;
                    refreshLayout.finishLoadmore();
                }

                if (proListBean.getProList().size() > 0) {
                    proListBBean.clear();
                    proListBBean.addAll(proListBean.getProList());
                    adapter.notifyDataSetChanged();
                    if(page == 2) {
                        iv_no_msg.setVisibility(View.GONE);
                    }
                } else {
                    refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                    if (page == 2) {
                        iv_no_msg.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    private void initListener() {
        fl_img.setOnClickListener(this);
       /* tvBlindDate.setOnClickListener(this);
        tvWedding.setOnClickListener(this);
        tvArticles.setOnClickListener(this);
        tvhoneymoon.setOnClickListener(this);*/
    }

    private void initView() {
        view = View.inflate(this, R.layout.fragment_visitingcard_head2, null);
        rv_type = view.findViewById(R.id.grid_view);
        tvTitleName = findViewById(R.id.title_textview);
        tvTitleName.setText(getIntent().getStringExtra("name"));
        fl_img = findViewById(R.id.fl_img);
        iv_no_msg = findViewById(R.id.iv_no_msg);
        listView = findViewById(R.id.marry_listview);
        refreshLayout = findViewById(R.id.refreshLayout);
        listView.addHeaderView(view);
      /*  tvBlindDate =view. findViewById(R.id.marry_blinddate);
        tvWedding =view. findViewById(R.id.marry_wedding);
        tvArticles = view.findViewById(R.id.marry_articles);
        tvhoneymoon =view. findViewById(R.id.marry_honeymoon);*/

        typeListAdapter = new VisitingTypeAdpter(this, typeList);
        rv_type.setNumColumns(5);
        rv_type.setAdapter(typeListAdapter);
        title_search = findViewById(R.id.title_search);
        title_search.setVisibility(View.VISIBLE);
        title_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MarryActivity.this, SearchActiviy.class);
                startActivity(intent);
            }
        });


        //最上面的模块
        rv_type.setOnItemClickListener((parent, view, position, id) -> {
            if (!TextUtils.isEmpty(typeList.get(position).getT_url())){
                Intent intent = new Intent(this, MarryActivity.class);
                intent.putExtra("type_id", typeList.get(position).getT_id());
                intent.putExtra("name", typeList.get(position).getT_name());
                startActivity(intent);
            }else {
                t_id = typeList.get(position).getT_id();
                page = 1;
                getProductList(true);
                typeListAdapter.clearSelection(position);
                typeListAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fl_img:
                finish();
                break;
          /*  case R.id.marry_blinddate://相亲
                Intent blinddate = new Intent(this,BlinddateActivity.class);
                startActivity(blinddate);
                break;
            case R.id.marry_wedding://婚礼
                Intent wedding = new Intent(this,WeddingActivity.class);
                startActivity(wedding);
                break;
            case R.id.marry_articles://婚品
                Intent articles = new Intent(this,ArticlesActivity.class);
                startActivity(articles);
                break;
            case R.id.marry_honeymoon://蜜月
                break;*/
        }
    }
}
